#include "widget.h"
#include "ui_widget.h"
#include <QDebug>
#include <QMessageBox>
#include <QNetworkDatagram>


//Widget::Widget(QWidget *parent)
//    : QWidget(parent)
//    , ui(new Ui::Widget)
//{
//    ui->setupUi(this);
//    this->setWindowTitle("服务器");
//    tcpServer = new QTcpServer(this);

//    //一定要先连接信号槽，在 监听 端口
//    connect(tcpServer,&QTcpServer::newConnection,this,&Widget::processNewConnection);
//    //绑定端口号
//    int listenret = tcpServer->listen(QHostAddress::Any,9999);
//    if(!listenret)
//    {
//        QMessageBox::critical(nullptr,"服务器启动出错",tcpServer->errorString());
//        return;
//    }
//}

//void Widget::processNewConnection()
//{
//    //1.获取新连接的socket
//    QTcpSocket* tcpSocket = tcpServer->nextPendingConnection();
//    QString log = QString("[") + tcpSocket->peerAddress().toString()+ ":" +
//            QString::number(tcpSocket->peerPort()) + "] 客⼾端上线!";
//    ui->listWidget->addItem(log);

//    //2.通过信号槽，处理新的socket
//    connect(tcpSocket,&QTcpSocket::readyRead,[=](){
//        QString request = tcpSocket->readAll();
//        QString response = process(request);
//        tcpSocket->write(response.toUtf8());
//        QString log = QString("[") + tcpSocket->peerAddress().toString()
//                      + ":" + QString::number(tcpSocket->peerPort()) + "] req: " +
//                      request + ", resp: " + response;
//        ui->listWidget->addItem(log);
//    });

//    //3.处理连接断开的请求
//    connect(tcpSocket,&QTcpSocket::disconnected,[=](){
//        QString log = QString("[") + tcpSocket->peerAddress().toString()
//                        + ":" + QString::number(tcpSocket->peerPort()) + "] 客⼾端下线!";
//        ui->listWidget->addItem(log);
//        //删除tcpSocket
//        tcpSocket->deleteLater();
//    });
//}

//const QString Widget::process(QString request)
//{
//    //复杂的处理过程...
//    return request;
//}

//Widget::~Widget()
//{
//    delete ui;
//}

//Widget::Widget(QWidget *parent)
//    : QWidget(parent)
//    , ui(new Ui::Widget)
//{
//    ui->setupUi(this);
//    this->setWindowTitle("服务器");
//    udpSocket = new QUdpSocket(this);

//    //一定要先连接信号槽，在绑定端口
//    //如果顺序反过来, 可能会出现端⼝绑定好了之后,
//    //请求就过来了. 此时还没来得及连接信号槽. 那么这个请求就有可能错过了.
//    connect(udpSocket,&QUdpSocket::readyRead,this,&Widget::processReq);

//    //绑定端口号
//    int bindret = udpSocket->bind(QHostAddress::Any,9999);

//    if(!bindret)
//    {
//        QMessageBox::critical(nullptr,"服务器启动出错",udpSocket->errorString());
//        return;
//    }
//}

//Widget::~Widget()
//{
//    delete ui;
//}

//void Widget::processReq()
//{
//    //1.读取请求并解析
//    QNetworkDatagram requestDatagram = udpSocket->receiveDatagram();
//    QString request = requestDatagram.data();
//    //2.根据请求做出响应
//    const QString response = process(request);
//    //3.发送响应
//    QNetworkDatagram responseDatagram(response.toUtf8(),requestDatagram.senderAddress(),requestDatagram.senderPort());
//    udpSocket->writeDatagram(responseDatagram);

//    QString log = "[" + requestDatagram.senderAddress().toString() + ":" + QString::number(requestDatagram.senderPort())
//            + "] request:" + request + " response:" + response;

//    ui->listWidget->addItem(log);

//}

//const QString Widget::process(QString request)
//{
//    //复杂的处理过程...
//    return request;
//}

